package com.sgq.vue.mybatis;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sgq.vue.entity.system.User;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;


@Component
public class MyBatisPlusAutoFillConfig  implements MetaObjectHandler {

    /**
     * 使用mp做添加操作时候，这个方法执行
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //设置属性值
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("version",0,metaObject);
        this.setFieldValByName("isDeleted",0,metaObject);
        if (metaObject.getOriginalObject().getClass() == User.class){
            this.setFieldValByName("isAccountNonExpired",1,metaObject);
            this.setFieldValByName("isAccountNonLocked",1,metaObject);
            this.setFieldValByName("isCredentialsNonExpired",1,metaObject);
            this.setFieldValByName("isEnabled",1,metaObject);
        }


    }

    /**
     * 使用mp做修改操作时候，这个方法执行
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
    }
}